import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/store'

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/user"
    },
    {
      name: '404',
      path: '/404',
      component: resolve => require(["@/views/basics/404"], resolve)
    }, {
      name: 'wechatLogin',
      path: '/wechatLogin',
      component: resolve => require(["@/views/basics/wechatLogin"], resolve)
    },
    {
      name: 'login',
      path: '/login',
      component: resolve => require(["@/views/basics/login"], resolve)
    },
    {
      name: 'feedback',
      path: '/feedback',
      component: resolve => require(["@/views/basics/feedback"], resolve)
    },
    {
      name: 'loan',
      path: '/loan',
      component: resolve => require(["@/views/user/loan/loan"], resolve),
      children:[
        {
          path: '/',
          redirect:'/loan/loanPlans',
        },  {
          name: 'loanPlans',
          path: 'loanPlans',
          component: resolve => require(["@/views/user/loan/loanPlans"], resolve),
        }, {
          name: 'addLoanPlan',
          path: 'addLoanPlan',
          component: resolve => require(["@/views/user/loan/addLoanPlan"], resolve),
        },{
          name: 'calLoanPlan',
          path: 'calLoanPlan',
          component: resolve => require(["@/views/user/loan/calLoanPlan"], resolve),
        },{
          name: 'loanPlan',
          path: 'loanPlan/:id',
          component: resolve => require(["@/views/user/loan/loanPlan"], resolve),
        },
      ]
    },
    {
      name: 'userHome',
      path: '/user',
      redirect: "/user/awesome",
      component: resolve => require(["@/views/user/home"], resolve),
      children: [
        {
          name: 'awesome',
          path: 'awesome',
          component: resolve => require(["@/views/user/awesome"], resolve),
          meta: { keepAlive: true }
        },
        {
          name: 'subscriptions',
          path: 'subscriptions',
          component: resolve => require(["@/views/user/subscriptions"], resolve)
        },
        {
          name: 'mine',
          path: 'mine',
          component: resolve => require(["@/views/user/mine"], resolve)
        },
        {
          name: 'positions',
          path: 'positions',
          component: resolve => require(["@/views/user/positions"], resolve)
        },
        {
          name: 'positionBusiness',
          path: 'positionBusiness',
          component: resolve => require(["@/views/user/positions/positionBusiness"], resolve)
        },
        {
          name: 'searchPositions',
          path: 'searchPositions',
          component: resolve => require(["@/views/user/positions/searchPositions"], resolve)
        },
        {
          name: 'userInfo',
          path: 'userInfo/:id',
          component: resolve => require(["@/views/user/userInfo"], resolve)
        }
      ]
    }

  ]
});
router.beforeEach((to, from, next) => {
  store.commit('updateLoadingStatus', true);
  /*if (to.name === 'login') {
    next();
  } else {
    if (localStorage.getItem('user_token')) {
      next()
    } else {
      next({
        path: '/login',
        replace: true
      })
    }
  }*/
  next();
});
router.afterEach((to, from) => {
  store.commit('updateLoadingStatus', false);
});

export default router;
